﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ScrumTime.Models.Story>" %>


    <style>
	    /* style the list to maximize the droppable hitarea */
	    #div-tasks ol { margin: 0; padding: 1em 0 1em 3em; }
	</style>


    
	<script>
	    function edittask(idtask, idstory, idAffectedTask) {
	        $.post('/Story/ShowStory_BasicData_Task_Edit',
                {
                    id: idtask,
                    idStory: idstory,
                    idAffectedTask: idAffectedTask
                },
                function (data) {
                    if (verificarError(data)) return;                    
                    $('#show-tasks').html(data);
                }
            );
        }

        function new_task(idstory) {
            edittask(-1, idstory, -1)
        }

        function addaffectedTask(affectedTaskId, idstory) {            
            edittask(-1, idstory, affectedTaskId);            
        }


        function showStory_deleteTask(taskId) {
            //delete task
            var storyid = $("#showStory_tasks_storyid").val();

            jConfirm('This action removes the element. Are you sure you want to continue?', 'Confirmation Dialog', function (r) {
                if (!r) return;

                $.post('/Task/Delete',
                {
                    id: taskId,
                    taskId: taskId,
                    storyId: storyid
                },
                function (data) {                    
                    if (verificarError(data)) return;
                    refreshTasksList();
                });
            });
        }


        function refreshTasksList() {            
            var storyid = $("#showStory_tasks_storyid").val();
            $.post('/Story/ShowStory_Tasks',
                {
                    id: storyid
                },
                function (data) {
                    if (verificarError(data)) return;
                    $("#div_main_show_story_taskList").html(data);
                }
            );
         }

        function editaffectedTask(affectedTaskId, description ) {
            // alert('editaffectedTask');
            var old_description = $('#showStoryTask-description').val();
            old_description = old_description + '\n' + '(added description)' + '\n';
            old_description = old_description + description;
            $('#showStoryTask-description').val(old_description);
            $('#ol-task-uaffectedtask').append('<li  id="li_at_' + affectedTaskId + '" affectedTaskId="' + affectedTaskId + '">' + description + ' <a href="#" onclick="remove_li_af(' + affectedTaskId + ')">&lt;remove&gt;</a></li>');
        }

        function remove_li_af(affectedTaskId) {
            $('#li_at_' + affectedTaskId).remove();            
        }

	    $(function () {	        
	        $("#catalog").accordion({
	            autoHeight: false,
	            navigation: true
	        });

//	        $("#catalog li").draggable({
//	            appendTo: "body",
//	            helper: "clone",
//	            revert: "invalid",
//                opacity: 0.35,
//                connectToSortable: "#sortable"
//	        });	        
	    });
	</script>

	
    <script type="text/C#" runat="server">
        private bool isAffectedTaskInStory(
                ScrumTime.Models.Story story,
                ScrumTime.Models.UsabilityAffectedTask affectedTask
            ) {

                bool resultado = false;

                foreach (ScrumTime.Models.Task task in story.Tasks) {
                    foreach (ScrumTime.Models.UsabilityAffectedTask uaffectedTask in task.UsabilityAffectedTasks) {
                        if (uaffectedTask.UsabilityAffectedTaskId == affectedTask.UsabilityAffectedTaskId) {
                            //resultado = true;
                            return true;
                        }
                    }
                }
            
                return resultado;            
        }
    </script>


    <input type="hidden" id="showStory_tasks_storyid" value="<%:Model.StoryId%>" />
    <table style="width:100%;">
        <tr>
            <td style="text-align:left; vertical-align:top; width:60%;">
                <div id="div-tasks">
                    
	                <h3 class="ui-widget-header" style="margin:0px; border:0px;">Tasks</h3>
                    
	                <div id="show-tasks" class="ui-widget" >                    
                    <div  class="ui-widget" style="border-width:1px;"> <a href="#" onclick="new_task(<%:Model.StoryId%>);">New task</a> </div>
                        <table style="width:100%;">
                                <tr>
                                    <td style="width:20px; font-weight:bold;">id</td>
                                    <td style="font-weight:bold;">Description</td>
                                    <td style="width:50px; font-weight:bold;">Hours</td>
                                    <td style="width:40px; font-weight:bold;">Acction</td>
                                </tr>
                        </table>
                        <table style="width:100%;">
                        <% foreach (ScrumTime.Models.Task task in Model.Tasks)
                           {
                          %>
                            <tr>
                            <% Html.RenderPartial("~/Views/Story/ShowStory_BasicData_Task_ReadOnly.ascx", task); %>    
                            </tr>
                        <% } 
                           %>
                        </table>
	                </div>
                </div>
            </td>
            <td style="text-align:left; vertical-align:top; width:60%;">            
                <div id="products">
                    <h3 class="ui-widget-header" style="margin:0px; border:0px;">Usability Affected Tasks</h3>
	                
	                <div id="catalog">

                    <% foreach (ScrumTime.Models.UsabilityFeature usabilityFeature in Model.UsabilityFeatures)
                       {%>

                            <h4><a href="#"> <%: usabilityFeature.Name %></a></h4>
                            
                            <div style="padding:0px;">
                                <ul>
                                <% foreach (ScrumTime.Models.UsabilityAffectedTask affectedTask in usabilityFeature.UsabilityAffectedTasks)
                                   { %>
                                    
                                        <li>
                                            <div>
                                                <% if (! isAffectedTaskInStory(Model, affectedTask))
                                                   { %>
                                                    <% if (affectedTask.Type.Contains("new") ) { %>
                                                    <a href="#" style="float:left" onclick="addaffectedTask(<%:affectedTask.UsabilityAffectedTaskId %>, <%:Model.StoryId%> );"title="add new task with affected task description">
                                                        <span class="ui-icon ui-icon-circle-plus"></span>
                                                    </a>
                                                    <% } %>
                                                
                                                    <% if (affectedTask.Type.Contains("edit") ) { %>
                                                    <a href="#"  style="float:left" onclick="editaffectedTask(<%:affectedTask.UsabilityAffectedTaskId %>, '<%:affectedTask.Description %>');" title="add affected task description">
                                                        <span class="ui-icon ui-icon-pencil"></span>
                                                    </a>
                                                    <% } %>

                                                <% } else{ %>
                                                    <span class="ui-icon ui-icon-circle-check" style="float:left" title="Implemented in a task" ></span>
                                                <% } %>
                                                <span><%:affectedTask.Description %> 
                                                    <a href="#" onclick="mostrarAyuda( <%:affectedTask.UsabilityAffectedTaskId%> , <%:affectedTask.HelpId%>, 'task');" class="ui-icon ui-icon-help" ></a>
                                                </span>
                                            </div>
                                            
                                             
                                        </li>
                                       
                                    
                                <% }%>
			                    </ul>
		                    </div>

                    <%
                       }
                        %>
		                
		                
	                </div>
                </div>

            </td>
        </tr>
    </table>







